package deckbuilder;
import java.io.*;
import java.util.*;
public class Deck 
{ 
	Vector<Card> flashcards = new Vector<Card>();
	
	public Deck()
	{
		
	}
	
	public void addCard(String t, String d)
	{
		Card c = new Card();
		c.setTerm(t);
		c.setDefinition(d);
		flashcards.add(c);
	}
	
	public Card getCard(int x)
	{
		return flashcards.elementAt(x);
	}
	
	public void printCard(int x)
	{
		System.out.println("Card" + x + ": " + flashcards.elementAt(x).getTerm() + ", " + flashcards.elementAt(x).getDefinition()+"\n");
	}
	
	public int size()
	{
		return flashcards.size();
	}
	
	public void buildDeckFromFile(String fileName)
	{		
		try
		{
			FileInputStream fstream = new FileInputStream(fileName);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			
			while ((strLine = br.readLine()) != null)   
			{
				addCard(strLine, br.readLine());
			}
			
			in.close();
		}catch (Exception e)
			{//Catch exception if any
		  
			System.err.println("Error: " + e.getMessage());
		
			}
	}
	
	public void saveDeckToFile(String fileName)
	{
		  try{
				  // Create file 
				  FileWriter fstream = new FileWriter(fileName);
				  BufferedWriter out = new BufferedWriter(fstream);
				  
				  for(int i = 0; i < flashcards.size(); i++)
				  {
					  out.write(flashcards.elementAt(i).getTerm());
					  out.write(flashcards.elementAt(i).getDefinition());
				  }
				  
				  //Close the output stream
				  out.close();
			  }catch (Exception e)
			  {//Catch exception if any
				  
				  System.err.println("Error: " + e.getMessage());
			  
			  }
	}
}
 